/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



function loadCalendar(selectedIndex, yearToLoad, monthToLoad){
    $.ajax({
        url: '../admin/calendar_actions.php',
        type: "POST",
        data: {kidId: selectedIndex, year: yearToLoad, month: monthToLoad, action: "readCalendarForKid"},
        dataType: "xml",
        success: function(data) {
            $("#kidcalendarmonth .monthcalendar").remove();

            var result = "<table class=\"monthcalendar\">";
            var prevCalYear = yearToLoad;
            var prevCalMonth = monthToLoad - 1;

            var nextCalYear = yearToLoad;
            var nextCalMonth = monthToLoad + 1;
            if(monthToLoad == 1){
                prevCalMonth = 12;
                prevCalYear -= 1;
            }else if (monthToLoad == 12) {
                nextCalMonth = 1;
                nextCalYear += 1;
            }
            result += "<tr><th><a href=\"javascript:loadCalendar("+selectedIndex+","+prevCalYear+","+prevCalMonth+")\">&lt;&lt;</a></th><th colspan=\"5\">"+$(data).find("calendarMonth").attr("monthLabel") + " " + $(data).find("calendarMonth").attr("year") +"</th><th><a href=\"javascript:loadCalendar("+selectedIndex+","+nextCalYear+","+nextCalMonth+")\">&gt;&gt;</a></th></tr>";
            result += "<tr><th>Ma</th><th>Di</th><th>Wo</th><th>Do</th><th>Vr</th><th>Za</th><th>Zo</th></tr>";
            result += "<tr>";

            var index = 1;
            $(data).find("calendarKids").each( function() {
                var calDayClass = "open";
                if($(this).attr("registeredDay") == "yes")
                    calDayClass = "registeredDay";
                result += "<td id=\"c_"+ $(this).attr("date") +"\" class=\""+calDayClass + " " + $(this).attr("itemType") + "\">" + $(this).attr("date").substring(6) + "</td>";
                if(index%7 == 0)
                    result += "</tr><tr>";
                index++;
            });
            result += "</tr>";
            result += "</table>";
            $(result).appendTo("#kidcalendarmonth #calendar");

            //set default start and stop
            $("#fromTime").attr("value", ($(data).find("calendarMonth").attr("defaultStart")));
            $("#toTime").attr("value", ($(data).find("calendarMonth").attr("defaultEnd")));

            //loop again over xml to add hover
            $(data).find("calendarKids").each( function() {
                if($(this).attr("registeredDay") == "yes"){
                    $("#c_" + $(this).attr("date")).bind("mouseover", {me:$(this)}, 
                        function (event) { 
                            var selectedDay = event.data.me.attr("date").substring(6);
                            $("#selectedTime").html("op " +selectedDay + ": <br/> " +event.data.me.attr("startTime") + " -> " + event.data.me.attr("endTime"));
                        });

                }
            });

            //registere event listener to open days of this month
            $("#kidcalendarmonth .open.this").click(function (){
                 addTimeToCalendar(this);
            });
        }

    });
    
    


}

function addTimeToCalendar(obj) {

    $.ajax({
        url: 'calendar_actions.php',
        type: "POST",
        data: {kidId: selectedKid, cDate: $(obj).attr("id").substring(2),  cStartTime: $("#fromTime").attr("value"), cEndTime: $("#toTime").attr("value"), action: "addTime"},
        dataType: "xml",
        success: function(data) {
            //$('.result').html(data);
            if($(data).find("result").attr("status") == "error"){
                $(".errormsg").html($(data).find("error").attr("descr"));
            }
            else if($(data).find("result").attr("status") == "ok"){
                //handle success
                var date = $(data).find("calendar").attr("date");
                $("#c_"+date).attr("class", "registered");

                loadCalendarNextWeek(selectedKid);
            }
        }
    });
}
